/* 区域及经销商选择相关js */
$(function(){

    App.init();

    var $addDealer = $('#addDealer'),
        $dealerPop = $('#dealerModal')

    //通用表格多选效果
    $("table td input[type='checkbox']").click(function(){
        var $span = $(this).parent();
        var $tr = $(this).closest("tr");
        if( $(this).prop("checked")){
            $tr.addClass("active");
            $span.addClass("checked");
        }else{
            $tr.removeClass("active");
            $span.removeClass("checked");
        }
    });
    $("table th input[type='checkbox']").click(function(){
        var $tr = $(this).closest("table").find("tbody tr");
        var $span = $(this).closest("table").find("tr .checker span");
        var $allRadio = $(this).closest("table").find("tbody :checkbox");
        if( $(this).prop("checked") ){
            $tr.addClass("active");
            $span.addClass("checked");
            $allRadio.prop("checked", true);
        }else{
            $tr.removeClass("active");
            $span.removeClass("checked");
            $allRadio.prop("checked", false);
        }
    });

    //选择所属经销商弹窗
    $addDealer.on("click", function(){
        $dealerPop.modal('show');
    });

    //表格初始化(清除选中)
    $dealerPop.on( 'hidden.bs.modal', function(e){
        $dealerPop.find( '.checker' ).children( 'span' ).removeClass( 'checked' ).find( ':checkbox' ).prop("checked", false).closest( 'tr' ).removeClass( 'active' );
    });

    //经销商选中赋值
    $('#dealerSubmit').on('click', function(){
        var $dealerChecked = $dealerPop.find('table tbody :checkbox:checked'),
            $dealerCheckedTr = $dealerChecked.closest( 'tr' ),
            $cloneTo = $( '#dealerList' ).find( 'tbody' ),
            $cloneToTr = $cloneTo.find('tr'),
            selectedArr = [];
        //拼装已选中经销商代码数组
        if( $cloneToTr.size() > 0 ){
            $cloneToTr.each(function( index, item ){
                var code = $.trim( $(item).find( 'td:eq(0)' ).text() );
                if( selectedArr.indexOf( code ) == -1 ){
                    selectedArr.push( code );
                };
            });
        };
        //添加未选中经销商
        $dealerCheckedTr.each(function( index, item ){
            var code = $.trim( $(item).find( 'td:eq(1)' ).text() ), $cur;
            //判断当前选中的经销商是否已存在已选中table中
            if( selectedArr.indexOf( code ) == -1 ){
                $cur = $(item).clone();
                $cur.find( 'td:eq(0)' ).remove();
                $cur.removeClass( 'active' ).append('<td><input type="button" class="btn blue btn-xs" value="删除"></td>')
                $cloneTo.append( $cur );        
            }
        });
        $dealerPop.modal('hide');
    });
    //选择地区弹窗树节点事件
    function zTreeOnClick2(event, treeId, treeNode) {
        $areaIput.val( treeNode.name );
        $areaPop.modal('hide');
        $areaClear.removeClass( 'disabled' );
    };
    //选择经销商弹窗树节点事件(todo)
    function zTreeOnClick(event, treeId, treeNode) {

    };
    var setting = {
        edit: {
            enable: true
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick
        }
    };
    var setting2 = {
        edit: {
            enable: true
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick2
        }
    };
    var zNodes =[
        { id:1, pId:0, name:"上海汽车工业(集团)总公司", open:true},
        { id:11, pId:1, name:"行政大区2区_MG",open:true},
        { id:111, pId:11, name:"雪佛兰",open:true},
        { id:1111, pId:111, name:"经销商礼遇"},
        { id:112, pId:11, name:"凯迪拉克",open:true,isParent:true},
        { id:12, pId:1, name:"行政大区1区_Rw",open:true},
        { id:121, pId:12, name:"制度类"},
        { id:122, pId:12, name:"流程类"},
        { id:123, pId:12, name:"话术类"}
    ];
    $.fn.zTree.init($("#companyTree"), setting, zNodes);
    $.fn.zTree.init($("#companyTree2"), setting2, zNodes);
})